/*
 * @Author: levi7754 levi7754@163.com
 * @Date: 2025-10-11 18:25:41
 * @LastEditors: levi7754 levi7754@163.com
 * @LastEditTime: 2025-10-13 17:40:35
 * @FilePath: \udp-nature-mall\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { init } from "@/api"

export default {
  state: {
    companyList: [],
    queryAllCustomer: false,
    toggleUser: 1,
    companyMap: {},
    saleOrgConfig: {},
    userInfo: {}, // 用户信息
    isLogin: !!uni.getStorageSync('kAuthorizedToken'), // 登录状态
  },
  getters: {
  },
  mutations: {
    setUserInfo(state, data) {
      state.userInfo = data;
      if (data.saleDepartmentVOList?.length) {
        if (data.saleDepartmentVOList[0].saleOrgConfigList?.length) {
          state.saleOrgConfig = data.saleDepartmentVOList[0]?.saleOrgConfigList[0]
        }
      }
      uni.setStorageSync('kUserInfo', data)
      uni.setStorageSync('saleOrgConfig', state.saleOrgConfig)
      if (data.accountType === "C") {
        uni.switchTab({url: "/pages/tabBar/goods/index"})
      } else {
        uni.switchTab({url: "/pages/tabBar/contact/index"})
      }
      state.isLogin = true
    },
  },
  actions: {
    
    // 获取个人信息
    async getInfo({commit, state}) {
      const token = uni.getStorageSync("kAuthorizedToken")
      const data  = await init({ userId: token.accountId, tenantId: 1 });
      if (!data) return;
      commit("setUserInfo", data)
      console.log({state})
    },
    // 重置用户默认数据
    resetUserData({state}) {
      uni.removeStorageSync('kAuthorizedToken');
      state.isLogin = false;
      state.userInfo = {};
      state.numData = 0;
      state.agentInfo = {};
      // cart().emptyList();
    },
    // 登录后
    async loginAfter() {
      app().getDict()
      systemApi.getDataAuth({code: 'ALL_CUSTOMER'}).then(res => {
        const token = uni.getStorageSync("token")
        this.queryAllCustomer = res.some(f => f.userId === token.accountId)
        console.log(this.queryAllCustomer)
      })
      await this.updateUserData();
      // cart().getList(); // 2025-05-16
      // this.getExpressCompanyList()

      // 登录后设置全局分享参数
      // $share.getShareInfo();
      // 提醒绑定手机号
      // if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
      //   showAuthModal('changeMobile');
      // }

      // 添加分享记录
      // const shareLog = uni.getStorageSync('shareLog');
      // if (!isEmpty(shareLog)) {
      //   this.addShareLog({
      //     ...shareLog,
      //   });
      // }
    },

    // 登出
    async logout(force = false) {
      // if (!force) {
      //   const { error } = await userApi.logout();
      //   if (error === 0) {
      //     this.resetUserData();
      //   }
      // }
      // if (force) {
      //   this.resetUserData();
      // }
      uni.removeStorageSync("token")
      uni.removeStorageSync("user-store")
      uni.removeStorageSync("searchHistory")
      uni.redirectTo("/pages/login/login")
      return !this.isLogin;
    },
  }
};